/*
  Copyright © 2014 Allen County Public Library.

  ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.
 * Author: James Harpe
 * 
 * 
 * Description: This adapter is responsible for the layout and content of the saved users in the SavedUserActivity.
 */

package us.in.lib.acpl.Mobile_ACPL.adapters;

import java.util.ArrayList;
import java.util.List;

import us.in.lib.acpl.Mobile_ACPL.R;
import us.in.lib.acpl.Mobile_ACPL.patron.SavedUser;
import us.in.lib.acpl.Mobile_ACPL.util.JSONArrayPrefs;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SavedUsersAdapter extends ArrayAdapter<SavedUser> {
	private Context context;
	private List<SavedUser> values;
	private SavedUsersAdapter thisAdapter = this;

	public SavedUsersAdapter(Context context, List<SavedUser> values) {
		super(context, R.layout.saved_user_list_item, values);
		this.context = context;
		this.values = values;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.saved_user_list_item, parent,
				false);

		TextView t = (TextView) rowView.findViewById(R.id.savedUserLine1);
		String nickname = values.get(position).getNickname();
		if (nickname.equals("")) {
			t.setText("ACPL Patron");
		} else {
			t.setText(nickname);
		}
		t = (TextView) rowView.findViewById(R.id.savedUserLine2);
		t.setText(values.get(position).getUser());

		rowView.findViewById(R.id.deleteUser).setOnClickListener(
				new DeleteUserListener(position));

		return rowView;

	}

	private class DeleteUserListener implements OnClickListener {
		private int position;

		public DeleteUserListener(int position) {
			this.position = position;
		}

		@Override
		public void onClick(View v) {
			AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
			builder.setTitle("Remove User");
			builder.setMessage("Are you sure you want to remove this saved user?");
			builder.setPositiveButton("Remove", new android.content.DialogInterface.OnClickListener(){

				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					arg0.dismiss();
					ArrayList<String> users = JSONArrayPrefs.getStringArrayPref(
							context, "savedUsers");
					ArrayList<String> pins = JSONArrayPrefs.getStringArrayPref(context,
							"savedPins");
					ArrayList<String> nicknames = JSONArrayPrefs.getStringArrayPref(
							context, "savedNicknames");
					ArrayList<String> remembers = JSONArrayPrefs.getStringArrayPref(
							context, "savedRemembers");
					users.remove(position);
					pins.remove(position);
					nicknames.remove(position);
					remembers.remove(position);
					JSONArrayPrefs.setStringArrayPref(context, "savedUsers", users);
					JSONArrayPrefs.setStringArrayPref(context, "savedPins", pins);
					JSONArrayPrefs.setStringArrayPref(context, "savedNicknames",
							nicknames);
					JSONArrayPrefs.setStringArrayPref(context, "savedRemembers",
							remembers);

					values.remove(position);
					thisAdapter.notifyDataSetChanged();
				}
				
			});
			
			builder.setNegativeButton("Cancel", new android.content.DialogInterface.OnClickListener(){

				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					arg0.dismiss();

				}
				
			});
			builder.create().show();
			

		}

	}

}
